//
//  BHCompetitionServiceManager.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/16.
//

import UIKit

class BHCompetitionServiceManager: NSObject {
    var gameId: String = ""
    // 赛事详情回调
    var responseForMainDetailBlock:((BHCompetitionDetailModel) -> Void)?
    // 赛事动态的回调
    var responseForDynBlock:((String,[BHCompetitionDynModel]) -> Void)?
    //报道的回调
    var responseForReportBlock:((Bool,String,[BHCompetitionReportModel]) -> Void)?

    
    //赛事签到的回调
    var responseForSignInBlock:((Bool,String) -> Void)?
    
    
}

extension BHCompetitionServiceManager {
    // 获取赛事详情 && 报名明细
    func loadCompetitionDetailService()  {
        
        let model = BHCompetitionDetailModel.init()
        
        let group = DispatchGroup.init()
        group.enter()
        let manager = BHHTTPClient.init()
        manager.get(url: String.game_detail_url, paramters: ["id":self.gameId]) { (success, errorMsg, responseDic) in
            if success == true {
                let resultDic = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                model.parseDic(resultDic)
            }
            group.leave()
        }
        
        group.enter()
        let applyManager = BHHTTPClient.init()
        applyManager.get(url: String.game_detail_applyDes_url, paramters: ["eventId":self.gameId]) { (success, errorMsg, responseDic) in
            if success == true {
                let resultDic = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                model.number = BHJsonTool.decodeIntFromDic(dic: resultDic, key: "number")
                model.remain = BHJsonTool.decodeIntFromDic(dic: resultDic, key: "remain")
                let array = BHJsonTool.decodeArrayFromDic(dic: resultDic, key: "urls")
                for item in array {
                    if item is NSNull {
                        
                    }else {
                        model.urls.append(item as! String)
                    }
                }
            }
            group.leave()
        }
        
        group.notify(queue: DispatchQueue.main) {
            self.responseForMainDetailBlock?(model)
        }
    }

    
    // 获取赛事动态
    func queryDynService()  {
        let manager = BHHTTPClient.init()
        manager.get(url: String.game_detail_dyn_url, paramters: ["eventId":self.gameId]) { (success, errorMsg, responseDic) in
            if success == true {
                var temp: [BHCompetitionDynModel] = []
                
                let resultDic = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                let livingUrl = BHJsonTool.decodeStringFromDic(dic: resultDic, key: "liveLink")
                let array = BHJsonTool.decodeArrayFromDic(dic: resultDic, key: "eventDynamic")
                for item in array {
                    let model = BHCompetitionDynModel.init()
                    model.parseDic(item as! [String:Any])
                    temp.append(model)
                }
                
                self.responseForDynBlock?(livingUrl,temp)
                
            }else {
                BHToast.toast(text: errorMsg)
            }
        }
    }
    
    // 获取赛事报道
    func queryReportService()  {
        let manager = BHHTTPClient.init()
        manager.get(url: String.game_detail_report_url, paramters: ["eventId":self.gameId]) { (success, errorMsg, responseDic) in
            if success == true {
                var temp: [BHCompetitionReportModel] = []
                
                let resultArray = BHJsonTool.decodeArrayFromDic(dic: responseDic, key: "result")
                for item in resultArray {
                    let model = BHCompetitionReportModel.init()
                    model.parseDic(item as! [String:Any])
                    temp.append(model)
                }
                self.responseForReportBlock?(true,"",temp)
            }else {
                BHToast.toast(text: errorMsg)
                self.responseForReportBlock?(false,errorMsg,[])
            }
        }
    }
    
    // 签到
    func gameSignService()  {
        var dic: [String:Any] = [:]
        
        dic["eventId"] = self.gameId
        dic["longitude"] = BHLocationManager.shared.longitude
        dic["latitude"] = BHLocationManager.shared.latitude

        let manager = BHHTTPClient.init()
        manager.post(url: String.game_detail_signIn_url, paramters: dic) { (success, errorMsg, responseDic) in
            self.responseForSignInBlock?(success,errorMsg)
        }
    }
}
